<template>
  <div class="workflow-chain" v-if="toolCalls && toolCalls.length > 0">
    <div class="chain-header">
      <span class="chain-icon">🧠</span>
      <span class="chain-title">思维链执行过程</span>
      <button 
        class="toggle-btn" 
        @click="expanded = !expanded"
        :class="{ expanded }"
      >
        {{ expanded ? '收起' : '展开' }}
      </button>
    </div>
    
    <div v-if="expanded" class="chain-steps">
      <!-- 检索步骤 -->
      <div class="chain-step retrieve-step">
        <div class="step-icon">🔍</div>
        <div class="step-content">
          <div class="step-title">步骤 0: 检索相关日志</div>
          <div class="step-description">检索到 {{ retrievedLogsCount }} 条相关日志</div>
        </div>
      </div>
      
      <!-- 工具调用步骤 -->
      <div 
        v-for="(call, index) in toolCalls" 
        :key="index"
        class="chain-step tool-step"
      >
        <div class="step-connector" v-if="index > 0"></div>
        <div class="step-icon">🔧</div>
        <div class="step-content">
          <div class="step-header">
            <span class="step-title">步骤 {{ call.iteration || (index + 1) }}: {{ getToolDisplayName(call.tool_name) }}</span>
            <span class="step-badge">{{ call.tool_name }}</span>
          </div>
          
          <!-- 思考过程 -->
          <div v-if="call.thought" class="step-thought">
            <div class="thought-label">💭 思考过程：</div>
            <div class="thought-content">{{ call.thought }}</div>
          </div>
          
          <!-- 工具结果摘要 -->
          <div class="step-result" v-if="call.result">
            <div class="result-label">📊 执行结果：</div>
            <div class="result-content">
              <pre>{{ formatResult(call.result) }}</pre>
            </div>
            <button 
              class="detail-btn"
              @click="toggleDetail(index)"
              v-if="hasDetailResult(call.result)"
            >
              {{ showDetails[index] ? '收起详情' : '查看详情' }}
            </button>
            <div v-if="showDetails[index]" class="result-detail">
              <pre>{{ JSON.stringify(call.result, null, 2) }}</pre>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 最终生成报告 -->
      <div class="chain-step final-step">
        <div class="step-connector"></div>
        <div class="step-icon">📝</div>
        <div class="step-content">
          <div class="step-title">最终步骤: 生成分析报告</div>
          <div class="step-description">整合所有工具结果，生成综合分析报告</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, defineProps } from 'vue';

const props = defineProps({
  toolCalls: {
    type: Array,
    default: () => []
  },
  retrievedLogsCount: {
    type: Number,
    default: 0
  }
});

const expanded = ref(false);
const showDetails = ref({});

// 工具名称映射
function getToolDisplayName(toolName) {
  const toolNames = {
    'analyze_error_frequency': '错误频率分析',
    'detect_anomalies': '异常检测',
    'extract_key_metrics': '关键指标提取',
    'analyze_trends': '趋势分析',
    'search_pattern': '模式搜索',
    'compare_logs': '日志对比',
    'generate_summary': '综合摘要',
    'analyze_time_patterns': '时间模式分析',
    'detect_slow_requests': '慢请求检测',
    'detect_security_issues': '安全问题检测',
    'statistical_analysis': '统计分析'
  };
  return toolNames[toolName] || toolName;
}

// 格式化结果（摘要）
function formatResult(result) {
  if (!result) return '无结果';
  
  if (typeof result === 'string') {
    return result.substring(0, 200) + (result.length > 200 ? '...' : '');
  }
  
  if (typeof result === 'object') {
    // 提取关键信息
    const summary = {};
    if (result.error) {
      return `错误: ${result.error}`;
    }
    if (result.total_errors !== undefined) {
      summary['错误总数'] = result.total_errors;
      summary['错误率'] = (result.error_rate * 100).toFixed(2) + '%';
    }
    if (result.anomaly_count !== undefined) {
      summary['异常数量'] = result.anomaly_count;
    }
    if (result.total_logs !== undefined) {
      summary['日志总数'] = result.total_logs;
    }
    if (result.match_count !== undefined) {
      summary['匹配数量'] = result.match_count;
    }
    if (result.peak_hour !== undefined) {
      summary['峰值时间'] = result.peak_hour + ':00';
    }
    if (result.slow_requests_count !== undefined) {
      summary['慢请求数'] = result.slow_requests_count;
    }
    if (result.total_security_issues !== undefined) {
      summary['安全问题数'] = result.total_security_issues;
      summary['风险级别'] = result.risk_level;
    }
    
    if (Object.keys(summary).length > 0) {
      return JSON.stringify(summary, null, 2);
    }
    
    return JSON.stringify(result, null, 2).substring(0, 200) + '...';
  }
  
  return String(result).substring(0, 200);
}

// 判断是否有详细结果
function hasDetailResult(result) {
  return result && typeof result === 'object' && !result.error;
}

// 切换详情显示
function toggleDetail(index) {
  showDetails.value[index] = !showDetails.value[index];
}
</script>

<style scoped>
.workflow-chain {
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f4ff 100%);
  border: 1px solid rgba(102, 126, 234, 0.2);
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 1rem;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.1);
}

.chain-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  font-weight: 600;
  color: #2c3e50;
}

.chain-icon {
  font-size: 1.2rem;
}

.chain-title {
  flex: 1;
}

.toggle-btn {
  background: rgba(102, 126, 234, 0.1);
  border: 1px solid rgba(102, 126, 234, 0.3);
  color: #667eea;
  padding: 0.25rem 0.75rem;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.toggle-btn:hover {
  background: rgba(102, 126, 234, 0.2);
}

.toggle-btn.expanded {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-color: transparent;
}

.chain-steps {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.chain-step {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  position: relative;
}

.step-connector {
  position: absolute;
  left: 21px;
  top: -1rem;
  width: 2px;
  height: 1rem;
  background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
}

.step-icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.retrieve-step .step-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.tool-step .step-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.final-step .step-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.step-content {
  flex: 1;
  background: white;
  border-radius: 10px;
  padding: 1rem;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.step-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.step-title {
  font-weight: 600;
  color: #2c3e50;
  font-size: 0.95rem;
}

.step-badge {
  background: rgba(102, 126, 234, 0.1);
  color: #667eea;
  padding: 0.2rem 0.6rem;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 500;
  font-family: monospace;
}

.step-description {
  color: #666;
  font-size: 0.85rem;
  margin-top: 0.25rem;
}

.step-thought {
  background: linear-gradient(135deg, #fff9e6 0%, #fff5d6 100%);
  border-left: 3px solid #ffd700;
  padding: 0.75rem;
  border-radius: 6px;
  margin-bottom: 0.75rem;
}

.thought-label {
  font-weight: 600;
  color: #856404;
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
  display: block;
}

.thought-content {
  color: #664d03;
  font-size: 0.9rem;
  line-height: 1.6;
  font-style: italic;
}

.step-result {
  background: #f8f9fa;
  border-radius: 6px;
  padding: 0.75rem;
}

.result-label {
  font-weight: 600;
  color: #495057;
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
}

.result-content {
  background: #282c34;
  color: #abb2bf;
  padding: 0.75rem;
  border-radius: 6px;
  margin-bottom: 0.5rem;
  overflow-x: auto;
}

.result-content pre {
  margin: 0;
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.detail-btn {
  background: rgba(102, 126, 234, 0.1);
  border: 1px solid rgba(102, 126, 234, 0.3);
  color: #667eea;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.detail-btn:hover {
  background: rgba(102, 126, 234, 0.2);
}

.result-detail {
  margin-top: 0.75rem;
  background: #282c34;
  color: #abb2bf;
  padding: 1rem;
  border-radius: 6px;
  max-height: 400px;
  overflow-y: auto;
}

.result-detail pre {
  margin: 0;
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
}
</style>

